<template>
	<view class="container">
		<cu-custom :bgLiner="true">
			<block slot="content">上传附件</block>
		</cu-custom>
		<view class="card">
			<view class="item flex justify-space-between align-center" v-for="(item,index) in FileList">
				<view class="width100 flex justify-start align-center">
					<view class="icon">
						<image v-if="item.type=== 'png' || item.type=== 'jpg' || item.type=== 'jpeg'"
							style="width: 71rpx;height: 91rpx;min-width: 71rpx;" src="/static/img/img-icon.png"></image>
						<image v-else-if="item.type=== 'pdf'" style="width: 71rpx;height: 91rpx;min-width: 71rpx;"
							src="/static/img/pdf-icon.png"></image>
						<image v-else-if="item.type=== 'xls'||item.type=== 'xlsx'" style="width: 71rpx;height: 91rpx;min-width: 71rpx;"
							src="/static/img/xls-icon.png"></image>
						<image v-else style="width: 71rpx;height: 91rpx;min-width: 71rpx;"
							src="/static/img/doc-icon.png"></image>
					</view>
					<view class="con">
						<view class="name text-overflow-1">{{item.name}}</view>
						<view class="size">{{item.size}}</view>
					</view>
				</view>
				<view @click="yulan(item,index)" class="yulan">预览</view>
				<view class="colse_icon flex align-center" @click="close(index)">
					<image src="/static/img/del-gray.png" style="min-width: 35rpx;width: 35rpx;height: 35rpx;"></image>
				</view>
			</view>
			<view class="upload">
				<view class="li">
					<uni-file-picker v-model="imageValue" file-mediatype="all"
						file-extname="xlsx,xls,doc,docx,pdf,png,jpg" @select="fileSelect"  :auto-upload="false"
						limit="9">
						<view class="upload_icon">
							<image class="add-s" src="/static/img/add-s.png"></image>
						</view>
					</uni-file-picker>
				</view>
				<!-- <image src="/static/img/add-s.png" class="add-s" @click="upImg"></image> -->
			</view>
		</view>
		<view style="height: 120rpx;"></view>
		<view class="footer-wrap">
			<view class="footer_btn" @click="upFileDo">
			    提交
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				windowHeight: 650,
				windowWidth: 375,
				FileList: [],
				suffix: {
					png:'/static/img/img-icon.png',
					jpg:'/static/img/img-icon.png',
					pdf:'/static/img/pdf-icon.png',
					doc:'/static/img/doc-icon.png',
					docx:'/static/img/doc-icon.png',
					xls:'/static/img/xls-icon.png',
					xlsx:'/static/img/xls-icon.png',
				},
				imageValue: [],
				id: 0,
			}
		},
		onLoad(option) {
			this.id = option.id;
			let annex_json = uni.getStorageSync('annex_json');
			if(annex_json){
				let annex_jsons = JSON.parse(annex_json);
				this.FileList = annex_jsons;
			}
		},
		methods: {
			yulan(item, index) {
				if(item.type == 'pdf'){
					uni.navigateTo({
						url:'/pages/system/web?link='+item.link
					})
				}else if(item.type == 'png'||item.type == 'jpg'||item.type == 'jpeg'){
					uni.previewImage({
						urls:[item.link],
						current:0
					})
				}else{
					// location.href = `https://view.officeapps.live.com/op/embed.aspx?src=${encodeURIComponent(item.link)}`;
					window.location.href = `${item.link}?x-oss-process=${item.type}/preview`
				}
			},
			close(index) {
				this.FileList.splice(index, 1);
			},
			fileSelect(e) { // 文件选择
				console.log(e)
				let tempFiles= e.tempFiles;
				this.upFile(tempFiles);
				// let type = file.tempFiles[0].extname
				// let li = {
				// 	name: file.tempFiles[0].name,
				// 	size: file.tempFiles[0].size,
				// 	type: file.tempFiles[0].extname
				// }
				// this.FileList.push(li)
			},
			upFile(tempFiles){
				if(tempFiles.length == 0){
					return
				};
				let file = tempFiles[0];
				let obj = {
					name:file.name,
					type:file.extname,
					size:this.$util.formatBytes(file.size)
				}
				this.$util.uploadFile(file.file,(e)=>{
					console.log(e)
					obj.link = e;
					this.FileList.push(obj);
					tempFiles.shift();
					this.upFile(tempFiles);
				},file.extname)
			},
			upFileDo(){
				let data = {
					id:this.id,
					annex_json:this.FileList.length?JSON.stringify(this.FileList):''
				}
				this.$http('/api/visitsAnnex', data).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						setTimeout(() => {
							this.$store.state.isUpDate = true;
							uni.navigateBack();
						}, 800)
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
				})
			},
			fileFail(file) { // 上传失败
				console.log('文件失败', file)
			},
			fileSuccess(file) { // 上传成功
				console.log('文件成功', file)
			},
		}
	}
</script>

<style lang="less" scoped>
	.container {
		padding-top: 30rpx;

		.upload {
			box-sizing: border-box;
			width: 150rpx;
			height: 150rpx;
			overflow: hidden;
			.add-s{
				width: 150rpx;
				height: 150rpx;
			}
			.li {
				width: 400rpx;
				height: 400rpx;
			}
		}

		.card {
			font-size: 28rpx;
			padding-left: 40rpx;
			padding-right: 20rpx;

			.item {
				padding: 0 20rpx;
				margin-bottom: 30rpx;

				.icon {
					margin-right: 30rpx;
				}

				.con {
					.name {
						font-size: 32rpx;
					}

					.size {
						color: #999;
					}
				}
				
				.yulan {
					min-width: 90rpx;
					// width: 80rpx;
					height: 49rpx;
					line-height: 45rpx;
					text-align: center;
					margin-right: 40rpx;
					color: #999;
					margin-left: 30rpx;
					border: 1px solid #DEDEDE;
					border-radius: 25rpx;
				}

				.colse_icon {
					min-width: 35rpx;
				}
			}
		}
	}
	.footer-wrap{
		width: 100%;
		background: #fff;
		position: fixed;
		bottom: 0;
		left: 0;
		padding: 10rpx 30rpx;
	}
	.footer_btn{
	  color: #ffffff;
	  background: #5cb9fb;
	  border-radius: 20rpx;  
	  height: 90rpx;
	  text-align: center;
	  line-height: 90rpx;
	  font-size: 34rpx;
	}
</style>